# -*- coding: utf-8 -*-
import scrapy
from JD.items import JdItem
from scrapy_redis.spiders import RedisSpider


class ComputerSpider(RedisSpider):
    name = 'computer'
    # allowed_domains = ['jd.com']
    # start_urls = ['https://search.jd.com/Search?keyword=手机&enc=utf-8&wq=手机']
    redis_key = 'huihui'

    def __init__(self, *args, **kwargs):
        domain = kwargs.pop("domains", "")
        self.alllowed_domains = filter(None, domain.split(','))
        print("lalala:", self.alllowed_domains)
        super(ComputerSpider, self).__init__(*args, **kwargs)

    def parse(self, response):
        # 定位所有电脑信息
        computer_list = response.xpath("//*[@class='gl-item']/div")
        # print(len(computer_list))
        item = JdItem()
        for computer in computer_list:
            item['详情'] = computer.xpath("./div[3]/a/em/text()").extract_first()
            item['价格'] = computer.xpath("./div[2]/strong/i/text()").extract_first()
            item['评价'] = computer.xpath("./div[4]/strong/a/text()").extract_first()
            item['商家'] = computer.xpath("./div[5]/span/a/text()").extract_first()
            item['标签'] = computer.xpath("./div[6]/i/text()").extract_first()
            item['链接'] = 'https://' + (computer.xpath("./div[3]/a/@href").extract_first()).split('//')[1]
            # print(item['link'])
            yield scrapy.Request(
                url=item['链接'],
                callback=self.parse_detail,
                meta={'hui': item}
            )
        # 翻页

        next_url = 'https://search.jd.com/Search?keyword=%E7%94%B5%E8%84%91&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq=%E7' \
                   '%94%B5%E8%84%91&page=$'.replace('$', str(i * 2 - 1 for i in range(1, 101)))
        print(next_url)
        if next_url:
            yield scrapy.Request(next_url, callback=self.parse)

    def parse_detail(self, response):
        # print(response.url)
        item = response.meta['hui']
        # print(item)
        list1 = response.xpath("//*[@class='p-parameter']")
        for i in list1:
            item['品牌'] = i.xpath('./ul[1]/li/text()').extract_first()
            item['名称'] = i.xpath('./ul[2]/li[1]/text()').extract_first()
            item['编号'] = i.xpath('./ul[2]/li[2]/text()').extract_first()
            item['毛重'] = i.xpath('./ul[2]/li[3]/text()').extract_first()
            item['产地'] = i.xpath('./ul[2]/li[4]/text()').extract_first()
            item['系统'] = i.xpath('./ul[2]/li[5]/text()').extract_first()
            item['厚度'] = i.xpath('./ul[2]/li[6]/text()').extract_first()
            item['内存'] = i.xpath('./ul[2]/li[7]/text()').extract_first()
            item['性能'] = i.xpath('./ul[2]/li[8]/text()').extract_first()
            item['待机'] = i.xpath('./ul[2]/li[9]/text()').extract_first()
            item['系列'] = i.xpath('./ul[2]/li[10]/text()').extract_first()
            item['重量'] = i.xpath('./ul[2]/li[11]/text()').extract_first()
            item['显卡'] = i.xpath('./ul[2]/li[12]/text()').extract_first()
            item['屏幕'] = i.xpath('./ul[2]/li[13]/text()').extract_first()
            item['显卡型号'] = i.xpath('./ul[2]/li[14]/text()').extract_first()
            item['色系'] = i.xpath('./ul[2]/li[15]/text()').extract_first()
            item['特性'] = i.xpath('./ul[2]/li[16]/text()').extract_first()
            item['显存'] = i.xpath('./ul[2]/li[17]/text()').extract_first()
            item['分辨率'] = i.xpath('./ul[2]/li[18]/text()').extract_first()
            item['服务'] = i.xpath('./ul[2]/li[19]/text()').extract_first()
            item['分类'] = i.xpath('./ul[2]/li[20]/text()').extract_first()
            item['处理器'] = i.xpath('./ul[2]/li[21]/text()').extract_first()
            item['硬盘'] = i.xpath('./ul[2]/li[22]/text()').extract_first()

        # print(item['computer_detail'])
        yield item
